package com.cd.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.Property;
import com.cd.bean.Science;
import com.cd.service.property.PropertyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 农村电商
 *@Author zhk
 *@Date 2018-1-2
 **/
@RequestMapping("property")
@Controller
public class PropertyController {
 
	@Autowired
	private PropertyService propertyService;
	
	
	/**
	 * 前台列表（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listProperty")
	public ModelAndView findAllProperty(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 8);//开始分页
	    List<Property> list = propertyService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("info/cytx/cytx");
		return mv;
	}
	 
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public ModelAndView findById(@RequestParam(value = "id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Property property = null;
		Property nextProperty = null;
		Property preProperty = null;
		int total = propertyService.size();//获取总记录
		int nextId = id+1;
		int preId = id-1;
		if(id > 0){
			if(id >= 1){
				property = propertyService.findById(id);//当前条
				preProperty = propertyService.findById(preId);//前一条
				nextProperty = propertyService.findById(nextId);//下一条	
			}else {
				property = propertyService.findById(id+1);
			}
		}else{
			property = propertyService.findById(id-1);
		}
		mv.addObject("property", property);
		mv.addObject("nextProperty", nextProperty);
		mv.addObject("preProperty", preProperty);
		mv.setViewName("info/cytx/cytx_show");
		return mv;
	}
	
	/**
	 * 产业体系列表（后台）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页,每页10条记录
	    List<Property> list = propertyService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/cytx/property_list");
		return mv;
	}
	/**
	 * 去添加页面
	 * @return
	 */
	@RequestMapping("/goAdd")
	public ModelAndView saveView(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("admin/cytx/property_add");
		return mv;
   }
	
	/**
	 * 新增
	 * @param title
	 * @param content
	 * @param level
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public ModelAndView save(@RequestParam(value="PROPERTY_TITLE")String title,
            @RequestParam(value="PROPERTY_CONTENT")String content,
            @RequestParam(value="PROPERTY_LEVEL")String level,@RequestParam(value="PROPERTY_RESOURCE")String resource) throws Exception {
		ModelAndView mv = new ModelAndView();
		//int total = propertyService.size();
		Property property = new Property();
		//property.setPROPERTY_ID(total+1);
		property.setPROPERTY_TITLE(title);
		property.setPROPERTY_CONTENT(content);
		property.setPUBLISH_DATE(new Date());
		property.setPROPERTY_LEVEL(level);
		property.setPROPERTY_RESOURCE(resource);
		propertyService.save(property);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		ModelAndView mv = new ModelAndView();
		propertyService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Property property = propertyService.findById(id);
		mv.addObject("property", property);
		mv.setViewName("admin/cytx/property_edit");
		return mv;
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param title
	 * @param content
	 * @param level
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="PROPERTY_ID")Integer id,@RequestParam(value="PROPERTY_TITLE")String title,
			                 @RequestParam(value="PROPERTY_CONTENT")String content) throws Exception {
			ModelAndView mv = new ModelAndView();
            Property property = propertyService.findById(id);
            property.setPROPERTY_ID(id);
            property.setPROPERTY_TITLE(title);
            property.setPROPERTY_CONTENT(content);
            //property.setPROPERTY_LEVEL(level);
            property.setPUBLISH_DATE(new Date());
            propertyService.edit(property);
			mv.setViewName("redirect:list.do");
		return mv;
	}

	/**
	 * 去查看内容页面(后台)
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goContent")
	public ModelAndView goContent(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Property property = propertyService.findById(id);
		mv.addObject("property", property);
		mv.setViewName("admin/cytx/property_content");
		return mv;
	}
	
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            propertyService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
}
